using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using A = Aspose;
using AB = Aspose.BarCode;

namespace BarCoder {
	public class Aspose {
		static Aspose() {
			AB.License lic = new A.BarCode.License();
			lic.SetLicense(("Aspose.BarCode.lic.xml"));
		}
		public static Dimok.BarCode.DS.BarCodes Read(string path) {
			Bitmap pic = new Bitmap(path);
			int pageCount = pic.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
			List<AB.BarCodeInfo> bciList = new List<A.BarCode.BarCodeInfo>();
			for (int page = 0; page < pageCount; page++) {
				pic.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, page);
				AB.BarCodeReader bcr = null;
				try {
					bcr = new A.BarCode.BarCodeReader(pic);
					bciList.InsertRange(bciList.Count, bcr.Read(AB.Symbology.Code128 | AB.Symbology.Code39Extended | AB.Symbology.Code39Standard | AB.Symbology.EAN13 | AB.Symbology.Interleaved2of5 | AB.Symbology.UPCA));
				}catch(Exception exc){
					if (!exc.Message.Contains("memory is corrupt.")) throw exc;
					break;
				} finally {
					if (bcr != null) bcr.Dispose();
				}
			}
			pic.Dispose();
			Dimok.BarCode.DS.BarCodes.BarCodeDataTable dtBarCodes = new Dimok.BarCode.DS.BarCodes().BarCode;
			foreach (AB.BarCodeInfo bci in bciList)
				dtBarCodes.AddBarCodeRow(bci.CodeText, (int)bci.SymbologyType, bci.SymbologyType + "");
			dtBarCodes.AddBarCodeRow(pageCount + "", (int)Dimok.BarCodeTypes.PageCount, Dimok.BarCodeTypes.PageCount + "");
			return (Dimok.BarCode.DS.BarCodes)dtBarCodes.DataSet;
		}
		public static Dimok.BarCode.DS.BarCodes ReadCode39(string path) {
			Bitmap pic = new Bitmap(path);
			int pageCount = pic.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
			List<AB.BarCodeInfo> bciList = new List<A.BarCode.BarCodeInfo>();
			for (int page = 0; page < pageCount; page++) {
				pic.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, page);

				Atalasoft.Imaging.ImageProcessing.ImageCommand cmd = new Atalasoft.Imaging.ImageProcessing.Filters.MedianCommand(2);
				//Atalasoft.Imaging.ImageProcessing.Filters.MeanCommand cmd = new Atalasoft.Imaging.ImageProcessing.Filters.MeanCommand(Atalasoft.Imaging.ImageProcessing.Filters.MeanFilterType.YP, 2);
				Atalasoft.Imaging.AtalaImage ai = Atalasoft.Imaging.AtalaImage.FromBitmap(pic);
				ai = cmd.ApplyToImage(ai);

				AB.BarCodeReader bcr = null;
				try {
					bcr = new A.BarCode.BarCodeReader(ai.BitmapMirror);
					bciList.InsertRange(bciList.Count, bcr.Read(AB.Symbology.Code39Extended | AB.Symbology.Code39Standard));
					if (bciList.Count > 0) break;
				} finally {
					if (bcr != null) bcr.Dispose();
					ai.Dispose();
				}
			}
			pic.Dispose();
			Dimok.BarCode.DS.BarCodes.BarCodeDataTable dtBarCodes = new Dimok.BarCode.DS.BarCodes().BarCode;
			foreach (AB.BarCodeInfo bci in bciList)
				dtBarCodes.AddBarCodeRow(bci.CodeText, (int)bci.SymbologyType, bci.SymbologyType + "");
			return (Dimok.BarCode.DS.BarCodes)dtBarCodes.DataSet;
		}
	}
}
